package com.click369.crm.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.click369.crm.common.ResultVo;
import com.click369.crm.pojo.CDept;
import com.click369.crm.pojo.CPosition;
import com.click369.crm.service.DeptService;
import com.click369.crm.service.PositionService;
import com.github.pagehelper.PageInfo;
/**
 * 职位的相关操作
 * @author Administrator
 *
 */
@Controller
@RequestMapping("position")
public class PositionController implements BaseController<CPosition>{
	
	@Autowired
	private PositionService positionService;
	
	@Autowired
	private DeptService deptService;
	
	//添加
	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultVo save(CPosition p) {
		p.setId(null);
		CDept cdept = deptService.getById(p.getDeptId());
		p.setDeptName(cdept.getDeptName());
		positionService.save(p);
		return new ResultVo(200,"增加成功");
	}
	
	//根据id删除
	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultVo delete(Integer id) {
		positionService.delete(id);
		return new ResultVo(200,"删除成功");
	}
	
	//修改
	@RequestMapping("update")
	@ResponseBody
	@Override
	public ResultVo update(CPosition p) {
		Integer id = p.getId();
		if(id == null){
			return new ResultVo(200,"没有id");
		}
		CPosition cposition = positionService.getById(id);
		p.setCreateTime(cposition.getCreateTime());
		p.setDeptName(cposition.getDeptName());
		positionService.update(p);
		return new ResultVo(200,"修改成功");
	}
	
	//查询所有分页
	@RequestMapping("list")
	@ResponseBody
	@Override
	public ResultVo select(@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="3")Integer rows, @Param(value="p")CPosition p) {
		PageInfo info = positionService.select(page, rows, p);
		return new ResultVo(info.getTotal(), info.getList());
	}
	
	//根据id查询
	@RequestMapping("getById")
	@ResponseBody
	@Override
	public ResultVo getById(Integer id) {
		CPosition byid = positionService.getById(id);
		return new ResultVo(byid);
	}
}
